A database functions as an organized collection of information. Electronic data may be stored in a database to facilitate rapid data retrieval. Data is typically organized within a database in one or more tables, with the tables being arranged as an array of rows and columns. A table can include a set of records, with each record including a set of fields. Records are typically indexed as rows within a table, and the fields are typically indexed as columns, such that a row/column pair of indices can reference particular datum within a table. For example, a row can store a complete data record relating to a sales transaction, a person, a project, etc. Likewise, columns of the table can define discrete portions of the rows that have the same general data format such that the columns define fields of the records. Database applications enable users to compare, sort, order, merge, separate, and interconnect data stored in databases.
A query may be issued to a database to access data stored therein. Queries in database systems are typically posed in high level, declarative (non-procedural) languages that are translated into a procedural execution plan. Numerous query languages exist, including SQL (Structured Query Language) and others. Many relational database management systems (RDBMSs) use query optimizers to select a query plan for a received query. The purpose of query optimization is to determine one or more ways in which the query can be translated into procedural plans, and to select the most efficient plan among the determined plans. The selected query execution plan typically includes a series of primitive database operators, and is typically selected according to a least estimated execution cost.
In RDBMSs, users issue queries against data that is stored in software constructs. When a query is received from a user, the RDBMS compiles the query by generating a query plan that can be executed against the database. As described above, for any such query, there are a variety of different query plans that can be appropriately used. For parameterized queries, different query plans may be more efficient depending on the particular values used for the parameters of the parameterized query. Some query plans will execute the query with a low cost, while other query plans will execute the query with a high cost. In order to avoid subsequent compilations of the same query, a query plan may be stored in memory so that the query plan can be quickly brought back for execution.
A particular query plan may be generated based on factors other than just the query text, such as available memory. Although each query plan may be valid across all values of those factors, plan reuse does not guarantee that an executed plan is currently optimal. Parameter values may impact the choice of access paths and the size of intermediate results, leading to different optimal query plans. One query plan generation strategy is to compile a query plan assuming parameter values submitted at the first execution. In this manner, an optimal query plan is used at the first execution. The compiled query plan may not be optimal for subsequent queries, however. Unfortunately, the use of suboptimal query plans may be prohibitive when queries process dramatically different numbers of rows due to different parameter values.